(() => {

    'use strict';
    goog.provide('Blockly.Blocks.ruilong');
    goog.require('Blockly.Blocks');



  Blockly.Arduino.forBlock['ruilong_emitter'] = function() {
   var ch = this.getFieldValue('CH');


   Blockly.Arduino.definitions_['_#include_RL_PCF8574T_h_'] =  '#include "RL_PCF8574T.h" ';
   Blockly.Arduino.definitions_['_PCF8574_pcf8574(0x20)_'] =  'PCF8574 pcf8574(0x20);//默认I2C地址为0X20';

   Blockly.Arduino.setups_['pcf8574_pinMode'] = 'pcf8574.begin();\n';
   Blockly.Arduino.setups_['pcf8574_pinMode'+ch] = 'pcf8574.pinMode(P'+ ch +', INPUT);\n';

   var code = 'pcf8574.digitalRead(P'+ch+')';
   return [code, Blockly.Arduino.ORDER_ATOMIC];
 };   

//初始化RFID
Blockly.Arduino.forBlock['ruilong_I2C_MFRC522_init']  = function () {
 var text_rfid_name = this.getFieldValue('rfid_name');
   // var value_PIN_SDA = Blockly.Arduino.valueToCode(this, 'PIN_SDA', Blockly.Arduino.ORDER_ATOMIC);
    //var value_PIN_RST = Blockly.Arduino.valueToCode(this, 'PIN_RST', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
    Blockly.Arduino.definitions_['include_MFRC522_I2C'] = '#include <MFRC522_I2C.h>';
    Blockly.Arduino.definitions_['var_declare_' + text_rfid_name] = 'MFRC522 ' + text_rfid_name + '(0x28, -1);';
    Blockly.Arduino.setups_['setup_Wire_begin'] = 'Wire.begin();';
    Blockly.Arduino.setups_['setup_mfc522_' + text_rfid_name] = text_rfid_name + '.PCD_Init();';
    var code = '';
    return code;
  };

//RFID侦测到信号
Blockly.Arduino.forBlock['ruilong_I2C_MFRC522_IsNewCard']  = function () {
 var text_rfid_name = this.getFieldValue('rfid_name');
 var statements_DO = Blockly.Arduino.statementToCode(this, 'DO');
 Blockly.Arduino.definitions_['function_MFRC522_IsNewCard'] = 'boolean MFRC522_IsNewCard(MFRC522 *_name){\n'
 + '  if(!_name->PICC_IsNewCardPresent())\n'
 + '    return false;\n'
 + '  if(!_name->PICC_ReadCardSerial())\n'
 + '    return false;\n'
 + '  return true;\n'
 + '}\n';
 var code = 'if(MFRC522_IsNewCard(&' + text_rfid_name + ')){\n'
 + (statements_DO != '' ? statements_DO : '')
 + '  ' + text_rfid_name + '.PICC_HaltA();\n'
 + '  ' + text_rfid_name + '.PCD_StopCrypto1();\n'
 + '}\n';
 return code;
};

//RFID读取卡号
Blockly.Arduino.forBlock['ruilong_I2C_MFRC522_ReadCardUID']  = function () {
 var text_rfid_name = this.getFieldValue('rfid_name');
 Blockly.Arduino.definitions_['function_MFRC522_ReadCardUID'] = 'String MFRC522_ReadCardUID(MFRC522 *_name){\n'
 + '  String _CardUID = "";\n'
 + '  for (byte _i = 0; _i < _name->uid.size; _i++){\n'
 + '    if(_name->uid.uidByte[_i] < 0x10)\n'
 + '      _CardUID += "0";\n'
 + '    _CardUID += String(_name->uid.uidByte[_i], HEX);\n'
 + '  }\n'
 + '  return _CardUID;\n'
 + '}\n';
 var code = 'MFRC522_ReadCardUID(&' + text_rfid_name + ')';
 return [code, Blockly.Arduino.ORDER_ATOMIC];
};

//RFID写卡
Blockly.Arduino.forBlock['ruilong_I2C_MFRC522_WriteCard'] = function () {
 var text_rfid_name = this.getFieldValue('rfid_name');
 var value_block = Blockly.Arduino.valueToCode(this, 'block', Blockly.Arduino.ORDER_ATOMIC);
 var value_buffer = Blockly.Arduino.valueToCode(this, 'buffer', Blockly.Arduino.ORDER_ATOMIC);
 var value_length = Blockly.Arduino.valueToCode(this, 'length', Blockly.Arduino.ORDER_ATOMIC);
 Blockly.Arduino.definitions_['function_MFRC522_WriteCard'] = 'boolean MFRC522_WriteCard(MFRC522 *_name, byte _block, byte *_buffer, byte _length){\n'
 + '  MFRC522::MIFARE_Key _key;\n'
 + '  for(byte i = 0; i < 6; i++)\n'
 + '    _key.keyByte[i] = 0xFF;\n'
 + '  MFRC522::StatusCode _status;\n'
 + '  _status =(MFRC522::StatusCode) _name->PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, _block, &_key, &(_name->uid));\n'
 + '  if(_status != MFRC522::STATUS_OK){\n'
 + '    Serial.print(F("PCD_Authenticate() failed: "));\n'
 + '    Serial.println(_name->GetStatusCodeName(_status));\n'
 + '    return false;\n'
 + '  }\n'
 + '  else{\n'
 + '    Serial.println(F("PCD_Authenticate() success;"));\n'
 + '  }\n'
 + '  _status = (MFRC522::StatusCode)_name->MIFARE_Write(_block, _buffer, _length);\n'
 + '  if(_status != MFRC522::STATUS_OK){\n'
 + '    Serial.print(F("MIFARE_Write() failed: "));\n'
 + '    Serial.println(_name->GetStatusCodeName(_status));\n'
 + '    return false;\n'
 + '  }\n'
 + '  else{\n'
 + '    Serial.println(F("MIFARE_Write() success;"));\n'
 + '  }\n'
 + '  return true;\n'
 + '}\n'
 Blockly.Arduino.setups_['setup_serial_Serial'] = 'Serial.begin(9600);';
 var code = 'MFRC522_WriteCard(&' + text_rfid_name + ', ' + value_block + ', ' + value_buffer + ', ' + value_length + ');\n';
 return code;
};

//RFID读卡
Blockly.Arduino.forBlock['ruilong_I2C_MFRC522_ReadCard'] = function () {
 var text_rfid_name = this.getFieldValue('rfid_name');
 var value_block = Blockly.Arduino.valueToCode(this, 'block', Blockly.Arduino.ORDER_ATOMIC);
 var value_buffer = Blockly.Arduino.valueToCode(this, 'buffer', Blockly.Arduino.ORDER_ATOMIC);
 var value_length = Blockly.Arduino.valueToCode(this, 'length', Blockly.Arduino.ORDER_ATOMIC);
 Blockly.Arduino.definitions_['function_MFRC522_ReadCard'] = 'boolean MFRC522_ReadCard(MFRC522 *_name, byte _block, byte *_buffer, byte _length){\n'
 + '  MFRC522::MIFARE_Key _key;\n'
 + '  for(byte i = 0; i < 6; i++)\n'
 + '    _key.keyByte[i] = 0xFF;\n'
 + '  MFRC522::StatusCode _status;\n'
 + '  _status = (MFRC522::StatusCode)_name->PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, _block, &_key, &(_name->uid));\n'
 + '  if(_status != MFRC522::STATUS_OK){\n'
 + '    Serial.print(F("PCD_Authenticate() failed: "));\n'
 + '    Serial.println(_name->GetStatusCodeName(_status));\n'
 + '    return false;\n'
 + '  }\n'
 + '  else{\n'
 + '    Serial.println(F("PCD_Authenticate() success;"));\n'
 + '  }\n'
 + '  if(_length < 18){\n'
 + '    byte _Read_buffer[18];\n'
 + '    byte _Read_buffer_length = 18;\n'
 + '    _status = (MFRC522::StatusCode)_name->MIFARE_Read(_block, _Read_buffer, &_Read_buffer_length);\n'
 + '    if(_status != MFRC522::STATUS_OK){\n'
 + '      Serial.print(F("MIFARE_Read() failed: "));\n'
 + '      Serial.println(_name->GetStatusCodeName(_status));\n'
 + '      return false;\n'
 + '    }\n'
 + '    else{\n'
 + '      Serial.println(F("MIFARE_Read() success;"));\n'
 + '    }\n'
 + '    for(byte _i = 0; _i < _length; _i++)\n'
 + '      _buffer[_i] = _Read_buffer[_i];\n'
 + '  }\n'
 + '  else{\n'
 + '    _status = (MFRC522::StatusCode)_name->MIFARE_Read(_block, _buffer, &_length);\n'
 + '    if(_status != MFRC522::STATUS_OK){\n'
 + '      Serial.print(F("MIFARE_Read() failed: "));\n'
 + '      Serial.println(_name->GetStatusCodeName(_status));\n'
 + '      return false;\n'
 + '    }\n'
 + '    else{\n'
 + '      Serial.println(F("MIFARE_Read() success;"));\n'
 + '    }\n'
 + '  }\n'
 + '  return true;\n'
 + '}\n'
 Blockly.Arduino.setups_['setup_serial_Serial'] = 'Serial.begin(9600);';
 var code = 'MFRC522_ReadCard(&' + text_rfid_name + ', ' + value_block + ', ' + value_buffer + ', ' + value_length + ');\n';
 return code;
};


  })();